package edu.tongji.jiaowu.dao.jdbc;

import java.util.ArrayList;
import java.util.List;

import edu.tongji.jiaowu.dao.VcodeDao;

import com.tongji.quickCRUDQ.define.Column;
import com.tongji.quickCRUDQ.define.Row;
import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.dao.jdbc.BaseDaoJdbc;

public class VcodeDaoJdbc extends BaseDaoJdbc implements VcodeDao {
	String tableName = "V_CODE";

	String beanName = "vcode";

	public CommonBean createVcode() {
		CommonBean vcodes = this.createBean(tableName, beanName);
		return vcodes;
	}

	/*
	 * public CommonBean findVcodes(String refid) { CommonBean
	 * vcodes=this.createVcode(); String sql="select * from V_CODE where
	 * refid=?"; List data=this.getCommonDao().select(sql,new String[]{refid});
	 * vcodes.addData(data); return vcodes; }
	 */
	public CommonBean findVcodeById(String id) {
		CommonBean vcodes = this.createVcode();
		String sql = "select * from V_CODE where id=?";
		List data = this.getCommonDao().select(sql, new String[] { id });
		vcodes.addData(data);
		return vcodes;
	}

	public CommonBean findAllVcodes() {
		CommonBean vcodes = this.createVcode();
		String sql = "select * from V_CODE ";
		List data = this.getCommonDao().select(sql, new String[] {});
		vcodes.addData(data);
		return vcodes;
	}

	public void insertVcode(CommonBean vcodeBean) {
		List data = vcodeBean.getData();
		Row row = this.getRow(tableName);
		this.getCommonDao().templateBatchInsert(row, data, this.getIdGenerator());
	}

	public CommonBean findCodes(String categoryCode) {
		CommonBean codes = this.createVcode();
		String sql = "select * from v_code where category_code=?";
		List data = this.getCommonDao().select(sql, new String[] { categoryCode });
		codes.addData(data);
		return codes;
	}

	public CommonBean findAllCodes(String categoryCode) {
		CommonBean codes = this.createVcode();
		Column statusColumn = Column.createColumn("STATUS", "status", Column.DATA_TYPE_VARCHAR, "使用状态", null, null);
		codes.addColumn(statusColumn);
		String sql = "select * from v_code_all where category_code=? order by code ";
		List data = this.getCommonDao().select(sql, new String[] { categoryCode });
		codes.addData(data);
		return codes;
	}

	public void status(String categoryCode, String[] codes, String isValid) {
		String status = "Y".equals(isValid) ? "1" : "0";
		if (codes == null || codes.length == 0) {// 更新所有代码的状态
			String sql = "update t_code set status=? where  exists(select * from t_code_category where category_code=?  and id=t_code.refid)";
			getCommonDao().updateRecord(sql, new String[] { status, categoryCode });
			return;
		}
		String sql = "update t_code set status=? where code=? and exists(select * from t_code_category where category_code=? and id=t_code.refid)";
		List dataArray = new ArrayList();
		for (int i = 0; i < codes.length; i++) {
			String[] params = new String[] { status, codes[i], categoryCode };
			dataArray.add(params);
		}
		getCommonDao().batchUpdateRecord(sql, dataArray);
	}

	public CommonBean findCodes(String categoryCode, String codeStr) {
		CommonBean codes = this.createVcode();
		String sql = "select * from v_code where category_code=? and code in(select * from dbo.f_split(?,','))";
		List data = this.getCommonDao().select(sql, new String[] { categoryCode,codeStr });
		codes.addData(data);
		return codes;
	}
}
